package com.zmzncs.lmtc.module.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.lmtc.module.customer.pojo.car.CarDTO;
import com.zmzncs.lmtc.module.shop.dao.ShopInfoDao;
import com.zmzncs.lmtc.module.shop.dao.ShopManagerShopDao;
import com.zmzncs.lmtc.module.shop.pojo.shop_info.ShopInfo;
import com.zmzncs.lmtc.module.shop.pojo.shop_info.ShopInfoDTO;
import com.zmzncs.lmtc.module.shop.pojo.shop_manager.*;
import com.zmzncs.lmtc.module.shop.dao.ShopManagerDao;
import com.zmzncs.lmtc.module.shop.pojo.shop_manager_shop.ShopManagerShop;
import com.zmzncs.lmtc.module.shop.service.ShopManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商户-管理员 服务实现类
 * </p>
 *
 * @author 高博文
 * @since 2021-05-27
 */
@Service
public class ShopManagerServiceImpl extends ServiceImpl<ShopManagerDao, ShopManager> implements ShopManagerService {

    @Autowired
    ShopManagerDao shopManagerDao;
    @Autowired
    ShopInfoDao shopInfoDao;
    @Autowired
    ShopManagerShopDao shopManagerShopDao;


    @Override
    public void add(ShopManagerAddForm form) {

        ShopManager manager = new ShopManager();
        manager.setUsername(form.getUsername());
        manager.setTelephone(form.getTelephone());
        manager.setIsShopkeeper(1);
        manager.setIsDeleted(0);
        manager.setPassword("000000");
        shopManagerDao.insert(manager);

    }

    @Override
    public ShopManagerDTO getByTelephone(String telephone) {
        return shopManagerDao.getManagerByTelephone(telephone);
    }

    @Override
    public IPage<ShopManagerDTO> getManagerPage(ShopManagerSelectForm form) {
        IPage<ShopManagerDTO> iPage = shopManagerDao.getManagerPage(form.getPage());
        if (iPage != null && iPage.getRecords() != null && iPage.getRecords().size() > 0) {
            for (ShopManagerDTO managerDTO : iPage.getRecords()) {
                List<ShopInfoDTO> shopInfoDTOS = shopInfoDao.getShopInfoByManagerId(managerDTO.getId());
                if (shopInfoDTOS != null && shopInfoDTOS.size() > 0){
                    managerDTO.setShopCount(shopInfoDTOS.size());
                    managerDTO.setShopList(shopInfoDTOS);
                }
            }
        }
        return iPage;
    }

    @Override
    public void delete(Long managerId) {
        ShopManager manager = new ShopManager();
        manager.setId(managerId);
        manager.setIsDeleted(1);
        shopManagerDao.updateById(manager);
    }

    @Override
    @Transactional
    public void updateManager(ShopManagerUpdateForm form) {

        ShopManager manager = new ShopManager();
        manager.setId(form.getManagerId());
        manager.setUsername(form.getUsername());
        manager.setTelephone(form.getTelephone());
        shopManagerDao.updateById(manager);

        shopManagerShopDao.deleteByManagerId(form.getManagerId());

        List<Long> shopIds = form.getShopIds();
        if (shopIds != null && shopIds.size() > 0) {
            for (Long shopId : shopIds) {
                ShopManagerShop managerShop = new ShopManagerShop();
                managerShop.setShopId(shopId);
                managerShop.setManagerId(form.getManagerId());
                managerShop.setIsDeleted(0);
                shopManagerShopDao.insert(managerShop);
            }
        }
    }




}
